#!/bin/bash

echo "1. 环境清理"
mkdir -p config
mkdir -p crypto-config
rm -rf ./config/*
rm -rf ./crypto-config/*
echo ""

echo "2. 生成组织基本结构"
cryptogen generate --config=./crypto-config.yaml
echo ""

echo "3. 生成初始区块文件"
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./config/genesis.block
echo ""

echo "4. 生成通道交易文件"
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./config/channel.tx -channelID mychannel
echo ""

echo "5. 生成锚节点更新文件"
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./config/OrgTest1MSPanchors.tx -channelID mychannel -asOrg OrgTest1MSP
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./config/OrgTest2MSPanchors.tx -channelID mychannel -asOrg OrgTest2MSP
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./config/OrgTest3MSPanchors.tx -channelID mychannel -asOrg OrgTest3MSP
echo ""

echo "6. 启动各节点"
docker-compose -f docker-compose-cli.yaml up -d
echo ""

echo "7. 创建通道"
docker exec cli peer channel create -o orderer.gdzce.cn:7050 -c mychannel -f ./config/channel.tx
echo ""

echo "8. 将各节点加入到通道中"
docker exec cli peer channel join -b mychannel.block
docker exec -e "CORE_PEER_LOCALMSPID=OrgTest1MSP" -e "CORE_PEER_ADDRESS=node1.organization1.gdzce.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization1.gdzce.cn/users/Admin@organization1.gdzce.cn/msp" cli peer channel join -b mychannel.block
docker exec -e "CORE_PEER_LOCALMSPID=OrgTest1MSP" -e "CORE_PEER_ADDRESS=node3.organization1.gdzce.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization1.gdzce.cn/users/Admin@organization1.gdzce.cn/msp" cli peer channel join -b mychannel.block
docker exec -e "CORE_PEER_LOCALMSPID=OrgTest2MSP" -e "CORE_PEER_ADDRESS=node1.organization2.gdzce.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization2.gdzce.cn/users/Admin@organization2.gdzce.cn/msp" cli peer channel join -b mychannel.block
docker exec -e "CORE_PEER_LOCALMSPID=OrgTest2MSP" -e "CORE_PEER_ADDRESS=node2.organization2.gdzce.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization2.gdzce.cn/users/Admin@organization2.gdzce.cn/msp" cli peer channel join -b mychannel.block
docker exec -e "CORE_PEER_LOCALMSPID=OrgTest2MSP" -e "CORE_PEER_ADDRESS=node3.organization2.gdzce.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization2.gdzce.cn/users/Admin@organization2.gdzce.cn/msp" cli peer channel join -b mychannel.block
docker exec -e "CORE_PEER_LOCALMSPID=OrgTest3MSP" -e "CORE_PEER_ADDRESS=node1.organization3.gdzce.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization3.gdzce.cn/users/Admin@organization3.gdzce.cn/msp" cli peer channel join -b mychannel.block
docker exec -e "CORE_PEER_LOCALMSPID=OrgTest3MSP" -e "CORE_PEER_ADDRESS=node2.organization3.gdzce.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization3.gdzce.cn/users/Admin@organization3.gdzce.cn/msp" cli peer channel join -b mychannel.block
docker exec -e "CORE_PEER_LOCALMSPID=OrgTest3MSP" -e "CORE_PEER_ADDRESS=node3.organization3.gdzce.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization3.gdzce.cn/users/Admin@organization3.gdzce.cn/msp" cli peer channel join -b mychannel.block
echo ""

echo "9. 安装链码"
docker exec cli peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/supplychain-finance
docker exec -e "CORE_PEER_LOCALMSPID=OrgTest2MSP" -e "CORE_PEER_ADDRESS=node2.organization2.gdzce.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization2.gdzce.cn/users/Admin@organization2.gdzce.cn/msp" cli peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/supplychain-finance
docker exec -e "CORE_PEER_LOCALMSPID=OrgTest3MSP" -e "CORE_PEER_ADDRESS=node2.organization3.gdzce.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization3.gdzce.cn/users/Admin@organization3.gdzce.cn/msp" cli peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/supplychain-finance
echo ""

echo "10. 实例化链码"
docker exec cli peer chaincode instantiate -o orderer.gdzce.cn:7050 -C mychannel -n mycc -l golang -v 1.0 -c '{"Args":["init"]}' -P "AND ('OrgTest1MSP.peer','OrgTest2MSP.peer','OrgTest3MSP.peer')"
echo "sleep 10"
sleep 10
echo ""

echo "11. 链码交互"
docker exec -e "CORE_PEER_LOCALMSPID=OrgTest2MSP" -e "CORE_PEER_ADDRESS=node2.organization2.gdzce.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization2.gdzce.cn/users/Admin@organization2.gdzce.cn/msp" cli peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
docker exec -e "CORE_PEER_LOCALMSPID=OrgTest3MSP" -e "CORE_PEER_ADDRESS=node2.organization3.gdzce.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization3.gdzce.cn/users/Admin@organization3.gdzce.cn/msp" cli peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
echo "启动成功"